{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 花式索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr3的值为：\n",
      " [[40 32 93 99  1]\n",
      " [94 68 23 64 38]\n",
      " [43 37 46  5 70]\n",
      " [87 16 73 88 37]\n",
      " [55 35  0 98 49]\n",
      " [51 44 56 25 55]\n",
      " [66 36 29 22 57]]\n",
      "arr3的花式索引：\n",
      " [[40 32 93 99  1]\n",
      " [43 37 46  5 70]\n",
      " [51 44 56 25 55]]\n",
      "使用负索引取arr3:\n",
      " [[66 36 29 22 57]\n",
      " [51 44 56 25 55]]\n",
      "使用多个元组花式索引：\n",
      " [93 88 55]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr3 = np.random.randint(100, size=(7, 5))\n",
    "print(\"arr3的值为：\\n\", arr3)\n",
    "print(\"arr3的花式索引：\\n\", arr3[[0, 2, 5]])\n",
    "\n",
    "print(\"使用负索引取arr3:\\n\", arr3[[-1, -2]])\n",
    "\n",
    "print(\"使用多个元组花式索引：\\n\", arr3[[0, 3, 5], [2, 3, 4]])\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 布尔索引\n",
    "创建一个3行4列的随机数组，将小于0.5的数值都设置为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.50254809 0.18256208 0.88834996 0.5050639 ]\n",
      " [0.3143366  0.09932439 0.03916006 0.36224856]\n",
      " [0.72655067 0.86527996 0.72737793 0.93175135]]\n",
      "bool索引为\n",
      " [[False  True False False]\n",
      " [ True  True  True  True]\n",
      " [False False False False]]\n",
      "清除小于0.5的数，结果为\n",
      " [[0.50254809 0.         0.88834996 0.5050639 ]\n",
      " [0.         0.         0.         0.        ]\n",
      " [0.72655067 0.86527996 0.72737793 0.93175135]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.random.rand(3, 4)\n",
    "print(a)\n",
    "a_bool = a < 0.5\n",
    "print(\"bool索引为\\n\", a_bool)\n",
    "a[a_bool] = 0\n",
    "print(\"清除小于0.5的数，结果为\\n\", a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 花式索引\n",
    "创建0-100的整数数组，大小为4行5列，打印数组\n",
    "（1）使用布尔索引获取第2行和第4行\n",
    "（2）使用花式索引获取第2行和第4行\n",
    "（3）使用花式索引获取（0,0）（1,4）（3,4）位置的数值\n",
    "（4）使用花式索引获取第2列和第4列\n",
    "\n",
    "思考是否可以使用普通索引获取第2行和第4行?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[33 18 94 17 31]\n",
      " [95 78 96 90 53]\n",
      " [57 20 92 51 23]\n",
      " [44 83 21 38 65]]\n",
      "使用布尔索引获取第2行和第4行\n",
      " [[95 78 96 90 53]\n",
      " [44 83 21 38 65]]\n",
      "使用花式索引获取第2行和第4行\n",
      " [[95 78 96 90 53]\n",
      " [44 83 21 38 65]]\n",
      "花式索引获取(0,0)(1,4)(3,4)位置\n",
      " [33 53 65]\n",
      "列的花式索引 [[18 17]\n",
      " [78 90]\n",
      " [20 51]\n",
      " [83 38]]\n",
      "正常索引\n",
      " [[95 78 96 90 53]\n",
      " [44 83 21 38 65]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.random.randint(low=0, high=100, size=(4, 5))\n",
    "print(a)\n",
    "print(\"使用布尔索引获取第2行和第4行\\n\", a[[False, True, False, True]])\n",
    "print(\"使用花式索引获取第2行和第4行\\n\", a[[1, 3]])\n",
    "print(\"花式索引获取(0,0)(1,4)(3,4)位置\\n\", a[(0, 1, 3), (0, 4, 4)]) # 注意元组！\n",
    "print(\"列的花式索引\", a[:, [1, 3]])\n",
    "\n",
    "print(\"正常索引\\n\", np.array([a[1], a[3]]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python-data-analysis",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
